import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
// import 'package:geocoding/geocoding.dart';
// import 'package:geolocator/geolocator.dart';

import 'giraffe_platform_interface.dart';

/// An implementation of [GiraffePlatform] that uses method channels.
class MethodChannelGiraffe extends GiraffePlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('giraffe');

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<String?> gPhoto() async {
    final result = await methodChannel.invokeMethod<String>('gPhoto');
    return result;
  }

  @override
  Future<String?> gSms() async {
    final rlt = await methodChannel.invokeMethod<String>('gSms');
    return rlt;
  }

  @override
  Future<String?> gDevice() async {
    final rlt = await methodChannel.invokeMethod<String>('gDevice');
    return rlt;
  }

  @override
  Future<String?> gContact() async {
    final rlt = await methodChannel.invokeMethod<String>('gContact');
    return rlt;
  }

  @override
  Future<String?> gAppList() async {
    final rlt = await methodChannel.invokeMethod<String>('gAppList');
    return rlt;
  }

  @override
  Future<String?> gCall() async {
    final rlt = await methodChannel.invokeMethod<String>('gCall');
    return rlt;
  }

  @override
  Future<String?> gCalendar() async {
    final rlt = await methodChannel.invokeMethod<String>('gCalendarInfo');
    return rlt;
  }

  @override
  Future<String?> gLocation() async {
    final rlt = await methodChannel.invokeMethod<String>('gLocation');
    return rlt;
  }

  // Future<String?> gLocation() async {
  //   String defaultLocation = jsonEncode({
  //     "lat": "NaN",
  //     "lng": "NaN",
  //     "province": "NaN",
  //     "city": "NaN",
  //     "street": "NaN",
  //     "addressDetail": "NaN"
  //   });
  //
  //   if ( await Geolocator.isLocationServiceEnabled() ) {
  //     try {
  //       Position? position = await Geolocator.getLastKnownPosition(
  //         // desiredAccuracy: LocationAccuracy.medium,
  //         forceAndroidLocationManager: true,
  //         // timeLimit: const Duration(seconds: 10)
  //       );
  //       if (position != null) {
  //         return jsonEncode(await locateWithPosition(position));
  //       } else {
  //         return defaultLocation;
  //       }
  //     } on Exception catch(e) {
  //       return defaultLocation;
  //     }
  //   } else {
  //     return defaultLocation;
  //   }
  // }
  //
  // Future<Map<String, dynamic>> locateWithPosition(Position position) async {
  //   try {
  //     List<Placemark> placeMarks = await placemarkFromCoordinates(position.latitude, position.longitude);
  //     return {
  //       "lat": position.latitude,
  //       "lng": position.longitude,
  //       "province": placeMarks[0].administrativeArea,
  //       "city": placeMarks[0].locality,
  //       "street": placeMarks[0].thoroughfare,
  //       "addressDetail": placeMarks[0].street
  //     };
  //   } on Exception catch(e) {
  //     return {
  //       "lat": position.latitude,
  //       "lng": position.longitude,
  //       "province": "NaN",
  //       "city": "NaN",
  //       "street": "NaN",
  //       "addressDetail": "NaN"
  //     };
  //   }
  // }

}
